<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="hardcode2013.*" %>
<%@ page import="java.util.List" %>

<html>
	<head>
		<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
		
		<script type="text/javascript">
			function itemAction(selValue, keyValue) {
				if (selValue != null) {
					document.getElementById("single_item_action").value = selValue;
					document.getElementById("single_item_key").value = keyValue;
					document.getElementById("item_form").submit();   
				}
			}
		</script>
	</head>
	
	<body>
		<%@include file="include_header.jsp" %>
		
		<%
		final String currentUrl = "/search.jsp";
		
		String searchProperty = request.getParameter("search_property");
		String searchValue = request.getParameter("search_value");
		String pageStr = request.getParameter("page");
		%>
		
		<fieldset>
			<legend>Search</legend>	
		    <form action="/item" method="get">
		      Search: <input type="text" name="search_value"><br>
		      Location: <select name="search_property">
			      <option value="title">Title</option>
			      <option value="description">Description</option>
			      <option value="price">Price</option>
		      </select><br>
		      <input type="hidden" name="action" value="search">
		      <input type="hidden" name="redirect_error" value="/search.jsp">
		      <input type="submit" value="Search" />
		    </form>
	    </fieldset>
	    
	    <%
		if (searchProperty != null && searchValue != null) {
			int currentPage = 1;
			if (pageStr != null) {
				try {
					currentPage = Integer.parseInt(pageStr);
				} catch (Exception e) {
				}
			}
			
			if (currentPage <= 0) {response.sendRedirect(currentUrl); return;}
			ItemServ.ItemQueryResult itemRes = ItemServ.searchItems(searchProperty, searchValue, currentPage, (profile != null && profile.getIsAdmin()), (profile != null ? profile.getUsername() : null));
			int totalPages = itemRes.totalItems / ItemServ.resultsPerPage + (itemRes.totalItems % ItemServ.resultsPerPage == 0 ? 0 : 1);
			if (currentPage > totalPages && itemRes.totalItems > 0) {response.sendRedirect(currentUrl + "?page=" + totalPages); return;}
			
			String pageLbl = "Total Items: " + itemRes.totalItems;
			if (itemRes.totalItems > 0)
				pageLbl += ", " +
				"Showing: " + ((currentPage - 1) * ItemServ.resultsPerPage + 1) + "-" + (currentPage * ItemServ.resultsPerPage > itemRes.totalItems ? itemRes.totalItems : currentPage * ItemServ.resultsPerPage) + ", " +
				"Page: " + currentPage + " / " + totalPages +
				(currentPage > 1 ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage - 1) + "\">Prev</a>" : "") +
				(currentPage < totalPages ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage + 1) + "\">Next</a>" : "");
			
			String legend = "Search results for " + "<b>" + EncodeUtil.encodeHtml(searchValue) + "</b>" + " in " +
				"<b>" + EncodeUtil.encodeHtml(searchProperty) + "</b>";
					
			%>
			<fieldset>
			<legend><%= legend %></legend>
				<%= pageLbl %>
				<form id="item_form" action="/item" method="post">
					<table border="1" width="100%">
					    <tr>
						    <td>Title</td>
					  	    <td>Description</td>
					   	    <td>Price</td>
							<td>Creation Date</td>
					   	    <td>Expiration Date</td>
					   	    <td>Seller</td>
					   	    <td>Action</td>
					    </tr>
						<%
						for (ItemProfile i : itemRes.itemList) {
							%>
							<tr>
							    <td><%= EncodeUtil.encodeHtml(i.getTitle()) %></td>
						  	    <td><%= EncodeUtil.encodeHtml(i.getDescription()).replaceAll("\n", "<br>") %></td>
						   	    <td>$<%= EncodeUtil.encodeHtml(i.getPrice()) %></td>
						   	    <td><%= EncodeUtil.encodeHtml(i.getCreationDateTime()) %></td>
						   	    <td><%= EncodeUtil.encodeHtml(i.getExpirationDateTime()) %></td>
						   	    <td><%= EncodeUtil.encodeHtml(i.getSeller()) %></td>
						   	    <td>
									<% if (profile != null) { %>
										<select onchange="itemAction(this.options[this.selectedIndex].value, <%= EncodeUtil.encodeHtml("\"" + i.getKeyStr() + "\"") %>)">
											<option value=null>Select Action</option>
											<% if (! ItemServ.matchSellerUser(i.getKeyStr(), profile.getUsername())) { %>
												<option value="mail">Mail</option>
											<% } %>
										  	<% if (profile.getIsAdmin() || ItemServ.matchSellerUser(i.getKeyStr(), profile.getUsername())) { %>
											  	<option value="edit">Edit</option>
										  	<% } %>
										  	<% if (profile.getIsAdmin() || ItemServ.matchSellerUser(i.getKeyStr(), profile.getUsername())) { %>
												<option value="delete">Delete</option>
										  	<% } %>
										</select>
									<% } %>
								</td>
						    </tr>
							<%
						}
						%>
					</table>
					<input type="hidden" id="single_item_action" name="single_item_action" value=null>
					<input type="hidden" id="single_item_key" name="single_item_key" value=null>
					<input type="hidden" id="action" name="action" value="single_item_action">
					<input type="hidden" name="redirect" value="<%= "/search.jsp?" + "search_property=" + EncodeUtil.encodeUrl(searchProperty) + "&search_value=" + EncodeUtil.encodeUrl(searchValue) +"&page=" + String.valueOf(currentPage) %>">
					<input type="hidden" name="auth_key" value="<%= SessionUtil.getAuthKey(request) %>">
				</form>
			</fieldset>
			<%
		}
		%>
	</body>
</html>